﻿using System;
using fastCSharp;

namespace diantou.web.onlineJudge
{
    /// <summary>
    /// 用户的提交记录
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPool = true, IsReferer = false)]
    partial class submitList : web.user.view<submitList>
    {
        /// <summary>
        /// 用户问题提交综合信息
        /// </summary>
        [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.problemJudge")]
        private sealed class problemJudge : diantou.dataAccess.judge.problemJudge
        {
            /// <summary>
            /// 用户问题标识
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public long Id;
            /// <summary>
            /// 问题
            /// </summary>
            public diantou.dataAccess.problem Problem;
            /// <summary>
            /// 
            /// </summary>
            public int Point;
            /// <summary>
            /// 用户标识
            /// </summary>
            public diantou.dataAccess.user User;
            /// <summary>
            /// 用户相关提交
            /// </summary>
            public userJudge UserJudge
            {
                get
                {
                    return new userJudge { Judge = Judge, UserId = User == null ? 0 : User.Id, IsAdmin = User != null && User.Permission == dataModel.user.permission.Admin };
                }
            }
            /// <summary>
            /// 用户问题提交综合信息
            /// </summary>
            /// <param name="value"></param>
            /// <param name="userId"></param>
            /// <param name="user"></param>
            public problemJudge(diantou.dataAccess.judge.problemJudge value, int userId, diantou.dataAccess.user user)
            {
                Id = ((long)userId << 32) + value.ProblemId;
                if ((Judge = value.Judge).SubmitTime != value.Time)
                {
                    Judge.SubmitTime = value.Time;
                    Judge.Id = -value.ProblemId;
                }
                Count = value.Count;
                Point = Judge.GetPoint(Problem = diantou.dataAccess.clientCache.Problem[value.ProblemId]);
                User = user;
            }
        }
        /// <summary>
        /// 用户提交问题列表
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        private problemJudge[] problems;
        /// <summary>
        /// 分组菜单
        /// </summary>
        private groupMenu groupMenu
        {
            get { return new groupMenu { ViewOnly = requestHeader.IsReView }; }
        }
        /// <summary>
        /// 用户的提交记录
        /// </summary>
        /// <param name="userId">用户标识</param>
        /// <param name="problemId">问题标识</param>
        /// <param name="page">分页号</param>
        /// <returns>是否成功</returns>
        private bool loadView(int userId, int problemId, int page)
        {
            if (base.load(userId, diantou.dataModel.path.pub.Default.OnlineJudgeStatusPath))
            {
                pageInfo.Size = fastCSharp.config.pub.Default.PageSize;
                pageInfo.Page = page;
                if (problemId == 0)
                {
                    diantou.dataAccess.judge.problemJudge[] problemJudges = diantou.dataAccess.tcpCall.judge.getUserProblems(userId, pageInfo.Size, pageInfo.Page, out pageInfo.Count);
                    problems = problemJudges.getArray(value => new problemJudge(value, userId, currentUser));
                    if (requestHeader.IsSearchEngine) pageInfo.Path = user.Path.OnlineJudgeSubmit.ToPageString();
                }
                else
                {
                    diantou.dataAccess.judge.problemJudge value = diantou.dataAccess.tcpCall.judge.getUserProblem(userId, problemId);
                    problems = value == null ? nullValue<problemJudge>.Array : new problemJudge[] { new problemJudge(value, userId, currentUser) };
                    pageInfo.Count = problems.Length;
                }
                return true;
            }
            return false;
        }
    }
}
